<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML5特征-拖拽-良好的用户体验</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            background-color: steelblue;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            overflow: hidden;
            margin: 0;
        }

        .empty {
            height: 150px;
            width: 150px;
            margin: 10px;
            border: 3px solid black;
            background: white;
        }

        .fill {
            width: 145px;
            height: 145px;
            cursor: pointer;
            background-color: green;
            background-image: url("https://source.unsplash.com/random/150x150");
        }

        .hold {
            border: 5px solid #ccc;
        }

        .hovered {
            background-color: #333;
            border-color: white;
            border-style: dashed;
        }
    </style>
</head>

<body>
    <div class="empty">
        <div class="fill" draggable="true"></div>
    </div>
    <div class="empty"></div>
    <div class="empty"></div>
    <div class="empty"></div>
    <div class="empty"></div>
</body>
<script>
    const fill = document.querySelector('.fill');
    const empties = document.querySelectorAll('.empty');

    fill.addEventListener('dragstart', dragStart);
    fill.addEventListener('dragend', dragEnd);

    for (let empty of empties) {
        empty.addEventListener('dragover', dragOver)
        empty.addEventListener('dragenter', dragEnter)
        empty.addEventListener('dragleave', dragLeave)
        empty.addEventListener('drop', dragDrop)
    }

    function dragOver(e) {
        e.preventDefault();
        console.log('over');
    }
    function dragEnter(e) {
        e.preventDefault();
        this.classList.add('hovered');
        console.log('enter');
    }
    function dragLeave() {
        this.classList.remove('hovered');

    }
    function dragDrop() {
        console.log('drop');
        this.className = 'empty'
        this.append(fill);
    }

    function dragStart() {
        this.classList.add('hold');
    }
    function dragEnd() {
        this.classList.remove('hold');
    }
</script>

</html>